Learning Flex Basics (Part 4): Working with Data Models
Table of Contents
- Introduction
- Create the Application Object and Application Layout
- Add the Form Controls and the DataGrid Controls
- Add the New Employee Model, Employee, and Location Models
- Instantiate the DataController Class, and Create the Employee and DataController Classes
- Add the DataController Methods
- Code Listing for data.mxml
- Code Listings for DataController.as and Employee.as
- Code Listings for locations.xml and employee.xml
- Where to Go From Here
Add the New Employee Model
The Model component in Flex is simple to implement but deceptively powerful. Here you use it to capture user input data, so that the handler method for the data can be decoupled from the actual form elements. The key to using the Model in this way to recognize that it generates an ActionScript object. By nesting XML tags inside the Model block, you are adding properties to the object. Therefore, the MXML and ActionScript code blocks below create essentially the same object:
MXML: <mx:Model id="myModel"> <name>Maya</name> <position>System Architect</position> </mx:Model> ActionScript: var myModel:Object=new Object(); myModel.name="Maya"; myModel.position="System Architect";
One of the advantages of using the MXML model here is that you can bind the
properties of the object to data values using curly braces: <name>{nameInput.text}</name>.
-
At the beginning of the
Applicationblock, add a Model withid="newEmpModel":<mx:Model id="newEmpModel"> </mx:Model>
-
Inside the Model block, add the properties
fName,lName, andlocationas nested XML tags:<fName></fName> <lName></lName> <location></location>
-
Bind the three XML tags to
fNameInput.text,lNameInput.text, andlocCombo.selectedItem:<fName>{fNameInput.text}</fName> <lName>{lNameInput.text}</lName> <location>{locCombo.selectedItem}</location>Note: These bindings are to properties of the Form controls you created earlier.
Add the Employee and Location Models
You now add two additional Model components that serve to import external data from XML files. Another benefit of the Model component is that it can take well-formed XML and automatically transform it transparently into an ActionScript data structure. To import an XML file, simply set the source attribute of the Model to the full or partial URL for the XML file.
- Open employees.xml or create it from the code listing. Note that inside the root node allEmployees contains two employee nodes with identical structures. These will become an array called employee that will be a property of the Model.
-
After the newEmpModel Model block, add a Model with
id="empModel"andsource="employees.xml":<mx:Model id="empModel" source="employees.xml"/>
- Open locations.xml or create it from the code listing.
Note that inside the root node locations contains a series of location
nodes. These will become an array called
locationthat will be a property of the Model. -
After the empModel, add another Model with
id="locModel"andsource="locations.xml":<mx:Model id="locModel" source="locations.xml"/>
Note: You created binding to these models when you created the ComboBox and empGrid DataGrid earlier. You may want to review this code to identify the bindings.